/**
 * Created by admin on 2016/10/13.
 */
import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class ReadWrite {
    //获取文件
    static void read(String filename, ArrayList<ArrayList> students, int[] scores){
        BufferedReader reader = null;
        String[] mediums;
        String medium;
        ArrayList student;
        Random rand = new Random();
        int score = 0, score1;

        //初始化reader
        try{
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "utf-8"));
        }catch(Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }

        //读文件，存到students
        try{
            reader.readLine();
            while((medium = reader.readLine()) != null){
                mediums = medium.split("\t");

                if(mediums.length==7)
                    for(int i = 2; i < 7; i++)
                    {
                        scores[i - 2] += Integer.parseInt(mediums[i]);
                        score += Integer.parseInt(mediums[i]);
                    }
                else
                    for(int i = 2; i < 7; i++)
                    {
                        score1 = (int)(25*rand.nextGaussian())+75;
                        score1 = score1 >100 ? 100:score1<50?50:score1;
                        medium += "\t" + score1  ;
                        scores[i - 2] += score1;
                        score += score1;
                    }

                student = new ArrayList();
                student.add(score);
                student.add(medium);
                students.add(student);
                //students.add(binarySearch(score),student);
                score = 0;
                scores[5]++;
            }
            reader.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //输出文件到output.txt
    static void write(String filename, ArrayList<ArrayList> students, int[] scores){
        BufferedWriter writer;
        try{
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename,true),"utf-8"));
            int length = students.size();
            if(filename == "sources/inputScore.txt"){
                for (int i = 5; i < length; i++) {
                    writer.write(students.get(i).get(1).toString() + "\n");
                }
            }else {
                writer.write("学号\t姓名\t语文\t英语\t数学\t化学\t物理\n");
                writer.newLine();
                for (int i = 0; i < length; i++) {
                    writer.write(students.get(i).get(1).toString() + "\t"+  students.get(i).get(0) + "\n");
                    writer.newLine();
                }
                writer.write("平均成绩");
                for (int i = 0; i < 5; i++)
                    writer.write("\t" + scores[i] / scores[5]);
            }
            writer.close();
        }catch(IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}
